﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeTester
{
    public class StringReverse
    {
        /// <summary>
        /// 反转所有字符
        /// </summary>
        /// <param name="arrChar"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public char[] Reverse(char[] arrChar,int start,int end)
        {
            if (arrChar == null || start < 0 || end > arrChar.Length - 1)
                return arrChar;

            while(start<end)
            {
                char temp = arrChar[start];
                arrChar[start] = arrChar[end];
                arrChar[end] = temp;

                start++;
                end--;
            }

            return arrChar;
        }

        /// <summary>
        /// 反转一个句子中的单词，单词本身的自母不反转
        /// </summary>
        /// <param name="sentence"></param>
        /// <returns></returns>
        public string ReverseSentence(string sentence)
        {
            if (string.IsNullOrEmpty(sentence))
                return sentence;

            char[] chars = sentence.ToArray();

            chars = this.Reverse(chars, 0, chars.Length - 1);//全部反转，包括单词的字母

            return sentence;
        }
    }
}
